package cn.game.tanke.role;

import cn.game.tanke.panel.TankPanel;
import cn.game.tanke.util.TankMap;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Bullet extends Role {
    private static final Logger LOGGER = Logger.getLogger(Bullet.class.getName());
    private boolean isGoOn = true;
    private int type; // 0:玩家子弹 1:敌人子弹
    private TankPanel panel;
    private Thread moveThread;

    public Bullet(int step, int x, int y, int type, int dir, TankPanel panel) {
        super(TankMap.bullet, 16, 16, step, dir, x, y);
        this.type = type;
        this.panel = panel;

        // 启动子弹移动线程
        moveThread = new Thread(this::moveContinuously);
        moveThread.start();
    }

    // 持续移动子弹
    private void moveContinuously() {
        while (isGoOn) {
            move();
            checkCollision();
            try {
                Thread.sleep(50); // 控制子弹移动速度
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    void annimal() {
        // 子弹不需要动画
    }

    @Override
    void move() {
        int x = getX();
        int y = getY();
        int dir = getDir();

        // 根据方向移动子弹
        switch (dir) {
            case 0: // 上
                if (isCanMove(x, y - 1)) {
                    setY(y - 1);
                } else {
                    isGoOn = false;
                }
                break;
            case 1: // 右
                if (isCanMove(x + 1, y)) {
                    setX(x + 1);
                } else {
                    isGoOn = false;
                }
                break;
            case 2: // 下
                if (isCanMove(x, y + 1)) {
                    setY(y + 1);
                } else {
                    isGoOn = false;
                }
                break;
            case 3: // 左
                if (isCanMove(x - 1, y)) {
                    setX(x - 1);
                } else {
                    isGoOn = false;
                }
                break;
        }
    }

    // 检查是否可以移动到指定位置
    private boolean isCanMove(int x, int y) {
        // 检查是否超出地图边界
        if (x < 0 || y < 0 || x >= TankMap.MAPARRAY[0].length || y >= TankMap.MAPARRAY.length) {
            return false;
        }

        // 检查地图上的障碍物类型
        int blockType = TankMap.MAPARRAY[y][x];
        switch (blockType) {
            case 3: // 砖墙
            case 4: // 钢墙
            case 5: // 水域
            case 6: // 基地
                return false;
            default:
                return true;
        }
    }

    // 检查碰撞
    public void checkCollision() {
        int x = getX();
        int y = getY();

        // 检查地图碰撞
        if (!isCanMove(x, y)) {
            handleMapCollision(x, y);
            return;
        }

        // 检查与玩家的碰撞（如果是敌人子弹）
        if (type == 1) {
            Player player = panel.getPlayer();
            if (player != null && player.getX() == x && player.getY() == y) {
                player.hit();
                isGoOn = false;
                return;
            }
        }

        // 检查与敌人的碰撞（如果是玩家子弹）
        if (type == 0) {
            for (Enemy enemy : panel.getEnemies()) {
                if (enemy != null && enemy.getX() == x && enemy.getY() == y) {
                    enemy.hit();
                    isGoOn = false;
                    return;
                }
            }
        }

        // 检查与boss的碰撞（如果是玩家子弹）
        if (type == 0) {
            Boss boss = Boss.getInstance();
            if (boss != null && boss.getX() == x && boss.getY() == y) {
                boss.hit(panel);
                isGoOn = false;
            }
        }
    }

    // 处理与地图的碰撞
    private void handleMapCollision(int x, int y) {
        if (x < 0 || y < 0 || x >= TankMap.MAPARRAY[0].length || y >= TankMap.MAPARRAY.length) {
            LOGGER.log(Level.INFO, "Bullet hit the boundary at ({0}, {1})", new Object[]{x, y});
            isGoOn = false;
            return;
        }

        int blockType = TankMap.MAPARRAY[y][x];
        switch (blockType) {
            case 3:
                LOGGER.log(Level.INFO, "Bullet destroyed a brick wall at ({0}, {1})", new Object[]{x, y});
                TankMap.MAPARRAY[y][x] = 1; // 移除砖墙
                break;
            case 7:
                LOGGER.log(Level.INFO, "Bullet hit the base at ({0}, {1})", new Object[]{x, y});
                TankMap.MAPARRAY[y][x] = 7; // 基地爆炸
                panel.gameOver(false); // 游戏结束（玩家失败）
                break;
            default:
                LOGGER.log(Level.INFO, "Bullet hit an obstacle at ({0}, {1})", new Object[]{x, y});
        }
        isGoOn = false;
    }

    @Override
    public void drawRole(Graphics graphics, JPanel jPanel) {
        if (!isGoOn) return;

        int x = getX() * 32;
        int y = getY() * 32;

        // 根据方向绘制子弹
        switch (getDir()) {
            case 0: // 上
                graphics.drawImage(TankMap.bullet, x, y, 16, 16, jPanel);
                break;
            case 1: // 右
                graphics.drawImage(TankMap.bullet, x, y, 16, 16, jPanel);
                break;
            case 2: // 下
                graphics.drawImage(TankMap.bullet, x, y, 16, 16, jPanel);
                break;
            case 3: // 左
                graphics.drawImage(TankMap.bullet, x, y, 16, 16, jPanel);
                break;
        }
    }

    public boolean isGoOn() {
        return isGoOn;
    }

    public int getType() {
        return type;
    }

    public void stopMoving() {
        isGoOn = false;
        moveThread.interrupt();
    }
}